package com.zqq.base.utils;

import android.os.Parcelable;
import android.text.TextUtils;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.Set;

public class MmkvUtils {

    private static volatile MmkvUtils mInstance;
    private static MMKV mmkv;

    private MmkvUtils() {
        mmkv = MMKV.defaultMMKV();
    }

    public static MmkvUtils getInstance() {
        if (mInstance == null) {
            synchronized (MmkvUtils.class) {
                if (mInstance == null) {
                    mInstance = new MmkvUtils();
                }
            }
        }
        return mInstance;
    }


    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param object
     */
    public void encode(String key, Object object) {
        try {
            if (TextUtils.isEmpty(key)) {
                return;
            }

            if (object instanceof String) {
                mmkv.encode(key, (String) object);
            } else if (object instanceof Integer) {
                mmkv.encode(key, (Integer) object);
            } else if (object instanceof Boolean) {
                mmkv.encode(key, (Boolean) object);
            } else if (object instanceof Float) {
                mmkv.encode(key, (Float) object);
            } else if (object instanceof Long) {
                mmkv.encode(key, (Long) object);
            } else if (object instanceof Double) {
                mmkv.encode(key, (Double) object);
            } else if (object instanceof byte[]) {
                mmkv.encode(key, (byte[]) object);
            } else {
                mmkv.encode(key, (Parcelable) object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取通过Parcelable序列化保存的对象
     *
     * @param key
     * @param mClass
     * @return
     */
    public <T extends Parcelable> T decodeObject(String key, Class<T> mClass) {
        return mmkv.decodeParcelable(key, mClass);
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    public Object decode(String key, Object defaultObject) {
        try {
            if (defaultObject instanceof String) {
                return mmkv.decodeString(key, (String) defaultObject);
            } else if (defaultObject instanceof Integer) {
                return mmkv.decodeInt(key, (Integer) defaultObject);
            } else if (defaultObject instanceof Boolean) {
                return mmkv.decodeBool(key, (Boolean) defaultObject);
            } else if (defaultObject instanceof Float) {
                return mmkv.decodeFloat(key, (Float) defaultObject);
            } else if (defaultObject instanceof Long) {
                return mmkv.decodeLong(key, (Long) defaultObject);
            } else if (defaultObject instanceof Double) {
                return mmkv.decodeDouble(key, (Double) defaultObject);
            } else if (defaultObject instanceof byte[]) {
                return mmkv.decodeBytes(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Set<String> decodeStringSet(String key) {
        return mmkv.decodeStringSet(key, Collections.<String>emptySet());
    }


    /**
     * 移除某个key对
     *
     * @param key
     */
    public void removeKey(String key) {
        mmkv.remove(key);
    }

    /**
     * 清除所有key
     */
    public void clearAll() {
        mmkv.clearAll();
    }
}
